<template>
  <el-form :model="ruleForm"  ref="ruleForm"  label-position="top" label-width="100px" class="demo-ruleForm" size="large" >

    <el-form-item label="请选择要上传图片ProductId" prop="productId">
      <el-select v-model="ruleForm.productId" placeholder="请选择productId ">
        <el-option v-for="product in products"
                   :label="product.productId"
                   :value="product.productId"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="Product头像上传(PNG格式)：" prop="headImg">
      <el-upload
          :action="uploadURL"
          list-type="picture-card"
          :class="{hide:hideUpload}"
          name="image"
          :on-preview="handlePreview"
          :http-request="headUpload">
        <i class="el-icon-plus"></i>
      </el-upload>
    </el-form-item>



  </el-form>
</template>

<script>
export default {
  data() {
    return {
      uploadURL:'',
      previewPath:'',
      previewVisible:'',
      limitCount:1,
      headFile: '',
      headFileList: '',
      hideUpload: false,
      headImg: [],//头像
      ruleForm: {},
      products:[{}]
    };
  },
  created() {
    const _this = this
    //此处需要查询item的全部信息
    axios({
      method:"GET",
      url:'http://115.29.176.82:8081/manager/api/product',
      params:{
        size:500,
        page:1
      }
    }).then(function (resp){
      _this.products = (resp.data.data.list)
    })

  },
  methods: {

    //图片上传
    headUpload(param) {
      let params = new FormData();
      params.append("image", param.file);
      console.log(param)
      axios({
        url: 'http://115.29.176.82:8081/manager/api/img/product/upload/'+this.ruleForm.productId,
        method: "POST",
        data: params,
        headers: {
          "Content-Type": "multipart/form-data"
        }
      }).then(res=> {
        console.log(res.data)
        this.headImg = [];
        this.headImg.push(res.data.data)

        if(res.status===200){
          alert('上传成功')
        }else{
          alert('上传失败')
        }

      })
    },

    //确认按钮
    certain() {
      const _this=this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          alert('上传成功');
        } else {
          console.log('上传失败');
          return false;
        }
      });
    },

    // 处理图片预览效果
    handlePreview(file) {
      this.previewPath = file.url;
      this.previewVisible = true
    },


  },

}
</script>